package work.huangxin.share.service.impl;

import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import work.huangxin.share.service.ISysFileService;
import work.huangxin.share.util.StringUtils;
import work.huangxin.share.util.file.DateUtils;
import work.huangxin.share.util.file.FileTypeUtils;

import work.huangxin.share.util.file.Seq;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Objects;

/**
 * @author: 张锦标
 * @date: 2023/3/26 19:46
 * ISysFileServiceImpl类
 */
@Service
public class ISysFileServiceImpl implements ISysFileService {

    public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension)
            throws IOException
    {   //判断文件名长度是否过长
        int fileNamelength = Objects.requireNonNull(file.getOriginalFilename()).length();
        if (fileNamelength > 100)
        {
            throw new RuntimeException("文件名过长！");
        }
        //判断文件的扩展类型是否合理
        assertAllowed(file, allowedExtension);
        //对文件名进行编码
        String fileName = extractFilename(file);
        //获取文件在本机的绝对地址
        String absPath = getAbsoluteFile(baseDir, fileName).getAbsolutePath();
        //将文件放到本机绝对地址
        file.transferTo(Paths.get(absPath));
        //返回文件名
        return getPathFileName(fileName);
    }
    private static final String getPathFileName(String fileName) throws IOException
    {
        String pathFileName = "/" + fileName;
        return pathFileName;
    }

    private static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException
    {
        File desc = new File(uploadDir + File.separator + fileName);

        if (!desc.exists())
        {
            if (!desc.getParentFile().exists())
            {
                desc.getParentFile().mkdirs();
            }
        }
        return desc.isAbsolute() ? desc : desc.getAbsoluteFile();
    }
    public static final String extractFilename(MultipartFile file)
    {
        return StringUtils.format("{}/{}_{}.{}", DateUtils.datePath(),
                FilenameUtils.getBaseName(file.getOriginalFilename()), Seq.getId(Seq.uploadSeqType), FileTypeUtils.getExtension(file));
    }

    private static void assertAllowed(MultipartFile file, String[] allowedExtension) {
        long size = file.getSize();
        if (size > 10 * 1024 * 1024)
        {
            throw new RuntimeException("文件大小不能超过10M");
        }
    }

    public static final String[] DEFAULT_ALLOWED_EXTENSION = {
            // 图片
            "bmp", "gif", "jpg", "jpeg", "png",
            // word excel powerpoint
            "doc", "docx", "xls", "xlsx", "ppt", "pptx", "html", "htm", "txt",
            // 压缩文件
            "rar", "zip", "gz", "bz2",
            // 视频格式
            "mp4", "avi", "rmvb",
            // pdf
            "pdf" };
    @Value("${file.path}")
    private String localFilePath;
    @Override
    public String uploadFile(MultipartFile file) throws Exception {
        String name = upload(localFilePath, file, DEFAULT_ALLOWED_EXTENSION);
        String url = localFilePath+name;
        return url;
    }
}
